package dao

import (
	"myapp/common"
	"myapp/model"
	"myapp/utils"

	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func InCar(c *gin.Context) {
	db := common.InitDB()
	var incars []*model.Incar
	err := db.Find(&incars).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "查询失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code: utils.SUCCESS,
			Data: gin.H{
				"incars": incars,
			},
		})
	}
}

func Get_Incar(c *gin.Context) {
	db := common.InitDB()
	pagenum, _ := strconv.Atoi(c.PostForm("pagenum"))
	pagesize, _ := strconv.Atoi(c.PostForm("pagesize"))

	var incars []*model.Incar
	var count int64
	err := db.Order("id").Limit(pagesize).Offset((pagenum - 1) * pagesize).Find(&incars).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "查询失败！",
		})
	}
	err = db.Table("incars").Count(&count).Error

	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "查询失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code: utils.SUCCESS,
			Data: gin.H{
				"incars": incars,
				"total":  count,
			},
		})
	}
}

func Get_id_Incar(c *gin.Context) {
	db := common.InitDB()
	id := c.Query("id")

	var incar model.Incar
	err := db.Where("id = ?", id).Find(&incar).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "查询失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code: utils.SUCCESS,
			Data: gin.H{
				"incar": incar,
			},
		})
	}
}

func Del_Incar(c *gin.Context) {
	db := common.InitDB()
	id := c.PostForm("id")

	err := db.Where("id = ?", id).Delete(&model.Incar{}).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "删除失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code:    utils.SUCCESS,
			Message: "删除成功！",
		})
	}

}
func Add_Incar(c *gin.Context) {
	db := common.InitDB()
	carID := c.PostForm("carID")
	inCount, _ := strconv.ParseInt(c.PostForm("inCount"), 10, 64)
	inWeight, _ := strconv.ParseFloat(c.PostForm("inWeight"), 64)
	orderID := c.PostForm("orderID")
	//时间转换
	indate, _ := time.ParseInLocation("2006-01-02", c.PostForm("inDate"), time.Local)

	var incar = model.Incar{CarID: carID, InCount: uint(inCount), InWeight: inWeight, OrderID: orderID,
		InDate: indate}

	err := db.Create(&incar).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "创建失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code:    utils.SUCCESS,
			Message: "创建成功！",
		})
	}
}
func Modify_INcar(c *gin.Context) {
	db := common.InitDB()
	id, _ := strconv.ParseInt(c.PostForm("id"), 10, 64)
	carID := c.PostForm("carID")
	inCount, _ := strconv.ParseInt(c.PostForm("inCount"), 10, 64)
	inWeight, _ := strconv.ParseFloat(c.PostForm("inWeight"), 64)
	orderID := c.PostForm("orderID")
	//时间转换
	indate, _ := time.ParseInLocation("2006-01-02", c.PostForm("inDate"), time.Local)
	var incar = model.Incar{ID: uint(id), CarID: carID, InCount: uint(inCount), InWeight: inWeight,
		OrderID: orderID,
		InDate:  indate}
	err := db.Model(&incar).Where("id = ?", incar.ID).Updates(incar).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "修改失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code:    utils.SUCCESS,
			Message: "修改成功！",
		})
	}
}
